package com.chare.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chare.pojo.Admin;
import com.chare.pojo.ChatMsg;
import com.chare.service.IAdminRoleService;
import com.chare.service.IAdminService;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import java.security.Principal;
import java.time.LocalDateTime;
import java.util.List;

@Controller
public class WsController {
    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;
@Autowired
private IAdminService adminService;
    @MessageMapping("/ws/chat")
    public void handleMsg(Authentication authentication, ChatMsg chatMsg){
      Admin admin= (Admin) authentication.getPrincipal();
        chatMsg.setFrom(admin.getUsername()); // 登录用户名
        chatMsg.setFromNickName(admin.getName()); // 显示用户名
        chatMsg.setDateTime(LocalDateTime.now());
        // 发送消息 参数：发送给谁，队列名，消息内容
        simpMessagingTemplate.convertAndSendToUser(chatMsg.getTo(),"/queue/chat",chatMsg);

    }
}
